package com.chen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.domain.Coin;
import com.chen.dto.CoinDto;
import com.chen.mapper.CoinMapper;
import com.chen.service.CoinService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CoinServiceImpl extends ServiceImpl<CoinMapper, Coin> implements CoinService {

    @Override
    public Coin findCoinByName(String coinName) {

        QueryWrapper<Coin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", coinName);

        List<Coin> list = list(queryWrapper);
        if (list == null) {
            return null;
        }
        return list.get(0);

    }

    @Override
    public Map<Long, CoinDto> getCoinByIds(List<Long> ids) {
        List<Coin> list = list(new LambdaQueryWrapper<Coin>().in(Coin::getId, ids));

        Map<Long, CoinDto> dtoMap = new HashMap<>();
        for(Coin coin : list){
            CoinDto coinDto = new CoinDto();
            BeanUtils.copyProperties(coin,coinDto);
            dtoMap.put(coin.getId(),coinDto);
        }

        return dtoMap;
    }
}
